import _ from "lodash";


function flatten(arr, childName, keyName, disabled) {
  var rets = [];

  for (var i = 0; i < arr.length; i++) {
    if (arr[i][childName] == undefined) {
      if (keyName == undefined) {
        !arr[i][disabled] && rets.push(arr[i])
      } else {
        !arr[i][disabled] && rets.push(arr[i][keyName])
      }
    } else {
      if (keyName != undefined) {
        !arr[i][disabled] && rets.push(arr[i][keyName])
      }
      rets = rets.concat(flatten(arr[i][childName], childName, keyName, disabled))
    }
  }
  return rets
}

export default {
  getPlatformId: (state) => {
    return state.uPlatformId
  },
  getAppInfo: (state) => {
    return function ({ pid, aid }) {
      let pObj = state.listApp.find((v) => { return v.id == pid })
      if (pObj == undefined) { return null }
      let aObj = pObj.list.find((v) => { return v.id == aid })
      if (aObj == undefined) { return null }
      return {
        pid: pid, pName: pObj.name,
        aid: aid, aName: aObj.name,
        isTop: aObj.isTop, isHide: aObj.isHide, icon: aObj.icon
      }
    }
  },//根据指定pid和aid获得应用关联的app数据，没有关联的返回null
  getCusInfo: (state) => {
    return function ({ pid, cusid }) {
      let list = state.cusList_Page_state.list
      let item = list.find((v) => {
        return (v.eSCompanyId == pid) && (v.id == cusid)
      })
      if (item == undefined) { return null }
      return {
        esid: item.eSCompanyId,
        cusid: item.id,
        isStar: item.isStar,
      }
    }
  },//根据指定pid和cusid关联的cus数据，没有关联的返回null
  getConditionalSelftag: (state) => {
    return {
      selftag: state.conditional.selftag,
      unselftag: state.conditional.unselftag
    }
  },

  getOpt: (state) => {
    let sc = state.conditional
    let joinTime = sc.isInTime ? {
      joinTimeBegin: sc.inTimeBegin, // 进线时间起始（不搜索不传）
      joinTimeEnd: sc.inTimeEnd, //进线时间终止
    } : {}
    return JSON.parse(JSON.stringify(Object.assign(
      {
        companyEmployeeId: sc.companyEmployeeId,
        fromSources: sc.fromSourceId || '', // 资源渠道（fromSourseId用","分隔）
        // isInTime: sc.isInTime,
        // inTimeBegin: sc.isInTime ?  : '',
        // inTimeEnd: sc.isInTime ? sc.incomingTime[1] : '',
        isStar: sc.isStar,
        readingType: sc.unread,
        replyType: sc.bubbling,
        isNoReply: sc.isOnlyNoReply,
        selftag: sc.selftag.join(","),
        unselftag: sc.unselftag.join(","),
      }, joinTime
    )))
  },
  getCEID: (state) => {
    return function (isValue) {
      if (isValue) {
        return state.conditional.companyEmployeeId;
      } else {
        let obj = {
          companyEmployeeId: state.conditional.companyEmployeeId,
        }
        return obj;
      }
    }
  },//获取员工(用户)id,常用
  getESID: (state) => {
    return function (isValue) {
      let id = state.uObjApp.pid || '';
      if (isValue) {
        return id
      } else {
        let obj = { eSCompanyId: id }
        return obj;
      }
    }
  },//获取平台id
  getAppID: (state) => {
    return function (isValue) {
      let pid = state.uObjApp.pid || '';
      let id = state.uObjApp.id || 0;
      if (isValue) {
        return id;
      } else {
        let obj = { eSCompanyId: pid, agentid: id }
        return obj;
      }
    }
  },//获取平台id,pid
  getCusId: (state) => {
    return function (isValue) {
      let id = state.uObjCus.id || 0;
      if (isValue) {
        return id
      } else {
        return { usersId: id }
      }
    }
  },//获取客户id


  getOrgTreeAllKey: (state) => {
    return flatten(state.treeOrg, 'children', 'id', 'disabled')
  },
  getPages: (state) => {
    let p = JSON.parse(JSON.stringify(state.cusList_Page_state.page));
    let c = state.cusList_Page_state.page.current
    p.page = c;
    return p
  },
  getConditional: (state) => {
    return state.conditional
  },
  getFromSourcesName: (state) => {
    return function (fromSources) {
      let arr;
      if (_.isString(fromSources)) {
        arr = fromSources.split(',')
      } else if (_.isArray(fromSources)) {
        arr = fromSources;
      } else {
        return [];
      }

      return arr.map((k) => {
        return state.fromSources.find((v) => {
          return v.fromSource_Id == k
        }).remark
      })
    }
  },
  getSelfTagsName: (state) => {
    let tagGroup = state.selfTags.reduce((t, v, i, a) => {
      t.push(v.selfTag)
      return _.flattenDeep(t);
    }, [])

    return function (SelfTags) {
      let arr;
      if (_.isString(SelfTags)) {
        arr = SelfTags.split(',').filter((v) => {
          return v != '';
        })
      } else if (_.isArray(SelfTags)) {
        arr = SelfTags;
      } else {
        return [];
      }
      return arr.map((k) => {
        return tagGroup.find((v) => {
          return v.selfTagId == k
        }).tagName
      })
    }
  }
}
